package com.ruoyi.web.controller.datastatistics;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.datastatistics.domain.TerminalHistoryData;
import com.ruoyi.datastatistics.service.ITerminalDbStatisticAnalysisService;
import com.ruoyi.datastatistics.service.ITerminalHistoryDataService;
import com.ruoyi.web.convert.terminalHistoryData.TerminalHistoryDataConvert;
import com.ruoyi.web.entity.RealtimeGatherData2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 大-历史数据Controller
 *
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/api")
public class TerminalHistoryDataController extends BaseController {
    @Autowired
    private ITerminalHistoryDataService terminalHistoryDataService;

    @Autowired
    private ITerminalDbStatisticAnalysisService terminalDbStatisticAnalysisService;

    /**
     * 查询大-历史数据列表
     * terminalId=628&searchTime=2024-01-21%2000%3A00%3A00%20-%202024-01-24%2023%3A59%3A59&timeSpan=0&page=1&rows=250&sidx=TerminalTime
     */
    @GetMapping("/RealtimeDataStatistic/GetRealtimeGatherDataLastForPage2")
    public TableDataInfo list(@RequestParam(value = "terminalId", required = false) Long terminalId,
                              @RequestParam(value = "locationNb", required = false) String locationNb,
                              @RequestParam(value = "searchTime", required = false) String searchTime,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime) throws ParseException {
        startPage();
        if (terminalId == 0) {
            return getDataTable(new ArrayList<TerminalHistoryData>());
        }

        if (null != searchTime && !searchTime.equals("")) {
            startTime = searchTime.split(" - ").length != 2 ? null : searchTime.split(" - ")[0];
            endTime = searchTime.split(" - ").length != 2 ? null : searchTime.split(" - ")[1];
        }
       /* if(null == startTime || startTime.isEmpty() || null == endTime || endTime.isEmpty()){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            endTime = simpleDateFormat.format(new Date());
            startTime = DateUtil.getLastDayEndDate(endTime);
        }*/
        List<RealtimeGatherData2> list = TerminalHistoryDataConvert.INSTANCE.convert(terminalHistoryDataService.getTerminalHistoryDataList(locationNb, startTime, endTime,null,null));

        return getDataTable(list);
    }

    @GetMapping("/leakage/NightDataContrastChartFreeForAll")
    public AjaxResult NightDataContrastChartFreeForAll(@RequestParam(name = "terminalId", required = false) Long terminalId, @RequestParam(name = "dateStart", required = false) String dateStart,
                                                       @RequestParam(name = "dateEnd", required = false) String dateEnd, @RequestParam(name = "chooseDate", required = false) String chooseDate,
                                                       @RequestParam(name = "chooseDate2", required = false) String chooseDate2, @RequestParam(name = "chooseDate3", required = false) String chooseDate3,
                                                       @RequestParam(name = "type", required = false) int type) {

        String[] time = {dateStart, dateEnd};
        String[] readDates = {chooseDate, chooseDate2, chooseDate3};
        String[] location_nb = {terminalId + ""};
        JSONArray json = terminalDbStatisticAnalysisService.oneTerminal2(location_nb, readDates, time, type);
        return success(json);
    }

    @GetMapping("/leakage/InstantaneousChar2")
    public TableDataInfo NightDataContrastChartFreeForAll2(@RequestParam(name = "terminalId", required = false) int terminalId,
                                                           @RequestParam(name = "locationNb", required = false) String locationNb,
                                                           @RequestParam(name = "searchTime", required = false) String searchTime) throws ParseException {
        String startTime = searchTime.split(" - ")[0] + " 00:00:00";
        String endTime = searchTime.split(" - ")[1] + " 23:59:59";
        List<RealtimeGatherData2> list = TerminalHistoryDataConvert.INSTANCE.convert(terminalHistoryDataService.getTerminalHistoryDataList(locationNb, startTime, endTime,null,null));
        return getDataTable(list);
    }

}
